﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SurehealthForms
{
    public partial class StockOrdering : Form
    {
        decimal Total=0;
        bool EditInProgress = false;

        public StockOrdering()
        {
            InitializeComponent();
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //if enter key pressed
            if (e.KeyChar == (char)13)
            {
                addItem();
            }
        }

        private bool QuantityMessageBox(string NumberCheck)
        {
            int Quantity=0;

            if (NumberCheck == "")
            {
                MessageBox.Show("Quantity Box is empty");
                return false;
            }

            try
            {
                Quantity=int.Parse(NumberCheck);
            }
            catch
            {
                MessageBox.Show("Quantity needs to be a number");
                return false;
            }
            if (Quantity < 0)
            {
                MessageBox.Show("Can't have a minus number");
                return false;
            }
            if (Quantity == 0)
            {
                MessageBox.Show("Can't have Zero Quantity");
                return false;
            }
          
            return true;
        }

        private void addItem()
        {
            decimal subTotal = 0;
            int Quanity = 0;

            if(QuantityMessageBox(QuanityBox.Text))
            {
                Quanity = int.Parse(QuanityBox.Text);
                ItemList.Items.Add(ItemDropDown.Text);
                QuantityList.Items.Add(QuanityBox.Text);
                subTotal = DodgyBobStockControl.Stock.StockQueryCost(ItemDropDown.Text) * Quanity;
                TotalList.Items.Add(subTotal);
                Total = subTotal + Total;
                TotalBox.Text = "£" + Total.ToString();
                QuanityBox.Text = "";
            }       
        }

        private void StockOrdering_Load(object sender, EventArgs e)
        {
            foreach (string s in DodgyBobStockControl.Stock.StockList())
            {
                ItemDropDown.Items.Add(s);
            }

        }

        private void StockOrder_Click(object sender, EventArgs e)
        {
            bool ObjectRestock = true;

            int index = 0;
            int indexOfFail = 0;

            foreach (object item in ItemList.Items)
            {
                if (!DodgyBobStockControl.Stock.RestockItem(item.ToString(), int.Parse(QuantityList.Items[index].ToString())))
                {
                    indexOfFail = index;
                    ObjectRestock = false;
                }

                index++;
            }
            if (ObjectRestock)
            {
                ItemList.Items.Clear();
                QuantityList.Items.Clear();
                TotalList.Items.Clear();
                TotalBox.Text = "£0.00";
                MessageBox.Show("Order has been successful");
            }
            else
                if (indexOfFail < index)
                {

                    MessageBox.Show("Order has Failed ");
                }
        }

        private void ReturnToMen_Click(object sender, EventArgs e)
        {
            PharmacistInit PharmacistInit = new PharmacistInit();
            this.Hide();
            PharmacistInit.Show();
        }

        private void EditItem_Click(object sender, EventArgs e)
        {
            int index=ItemList.SelectedIndex;
            EditInProgress = true;
            if (index != -1)
            {
                SaveItem.Enabled = true;
                ItemDropDown.Text = ItemList.Items[index].ToString();
                QuanityBox.Text = QuantityList.Items[index].ToString(); 
            }

        }

        private void StockAddItem_Click(object sender, EventArgs e)
        {
            addItem();
        }

        private void RemoveItem_Click(object sender, EventArgs e)
        {
            int index = ItemList.SelectedIndex;
            if (index != -1)
            {
                Total = Total - decimal.Parse(TotalList.Items[index].ToString());
                TotalBox.Text = "£" + Total;
                ItemList.Items.RemoveAt(index);
                QuantityList.Items.RemoveAt(index);
                TotalList.Items.RemoveAt(index);
                SaveItem.Enabled = false;
                EditInProgress = false;

            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            int index = ItemList.SelectedIndex;
            decimal oldSubTotal = decimal.Parse(TotalList.Items[index].ToString());
            decimal subTotal = 0;
            int Quanity = 0;

            if (QuantityMessageBox(QuanityBox.Text))
            {

                Total = Total - oldSubTotal;
                Quanity = int.Parse(QuanityBox.Text);
                QuantityList.Items[index] = QuanityBox.Text;
                subTotal = DodgyBobStockControl.Stock.StockQueryCost(ItemDropDown.Text) * Quanity;
                TotalList.Items[index] = subTotal;
                Total = Total + subTotal;
                TotalBox.Text = "£" + Total.ToString();

                QuanityBox.Text = "";

                EditInProgress = false;
            }
        }

        private void ItemList_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ItemList.SelectedIndex == -1)
            {
                EditItem.Enabled = false;
                RemoveItem.Enabled = false;
            }
            else
            {
                EditItem.Enabled = true;
                RemoveItem.Enabled = true;
            }
        }

        private void ItemDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (EditInProgress == false)
            {
                SaveItem.Enabled = false;
                EditItem.Enabled = false;
                ItemList.SelectedIndex = -1;
                QuanityBox.Text = "";
            }
            else
            {
                SaveItem.Enabled = true;
                EditItem.Enabled = true;
            }
            
        }

        private void OutOfStock_Click(object sender, EventArgs e)
        {
            bool OutOfStock = false;
            foreach (object o in DodgyBobStockControl.Stock.StockList())
            {
                int stock = DodgyBobStockControl.Stock.StockCheck(o.ToString());
                if (stock == 0)
                {
                    //Console.WriteLine(o.ToString());
                    ItemList.Items.Add(o);
                    QuantityList.Items.Add("0");
                    TotalList.Items.Add("0.00");
                    OutOfStock = true;
                }
                 
            }
            if (!OutOfStock)
                MessageBox.Show("No Items are out of stock");
        }

        private void OutOfDate_Click(object sender, EventArgs e)
        {
            bool OutOfDate = false;
            foreach (object o in DodgyBobStockControl.Stock.StockList())
            {
                if(!DodgyBobStockControl.Stock.CheckInDate(o.ToString()))
                {
                    ItemList.Items.Add(o);
                    QuantityList.Items.Add("0");
                    TotalList.Items.Add("0.00");
                    OutOfDate = true;
                }
            }
            if (!OutOfDate)
                MessageBox.Show("No Items are out of date");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DodgyBobStockControl.Stock.UseStock("Bloaty Head pin", DodgyBobStockControl.Stock.StockCheck("Bloaty Head pin"));
        }

   

    }
}
